package com.owbtgad.common

import android.app.Application
import android.util.Log
import androidx.lifecycle.ProcessLifecycleOwner
import com.alibaba.android.arouter.launcher.ARouter
import com.jeremyliao.liveeventbus.LiveEventBus
import com.owbtgad.common.utils.BaseAppLication
import com.owbtgad.common.utils.LiveEventBusString
import com.tencent.mmkv.MMKV
import com.xj.anchortask.library.AnchorTask
import com.xj.anchortask.library.IAnchorTaskCreator

/**
 * Created by jun xu on 2/1/21.
 */

const val TASK_NAME_ZERO = "zero"
const val TASK_NAME_ONE = "one"
const val TASK_NAME_TWO = "two"
const val TASK_NAME_THREE = "three"
const val TASK_NAME_FOUR = "four"
const val TASK_NAME_FIVE = "five"

class ApplicationAnchorTaskCreator(val application: Application) : IAnchorTaskCreator {
    override fun createTask(taskName: String): AnchorTask? {
        when (taskName) {
            TASK_NAME_ZERO -> {
                return AnchorTaskZero(application)
            }

            TASK_NAME_ONE -> {
                return AnchorTaskOne(application)
            }
            TASK_NAME_TWO -> {
                return AnchorTaskTwo(application)
            }
            TASK_NAME_THREE -> {
                return AnchorTaskThree()
            }
            TASK_NAME_FOUR -> {
                return AnchorTaskFour()
            }
            TASK_NAME_FIVE -> {
                return AnchorTaskFive()
            }
        }
        return null
    }

}

class AnchorTaskZero(val context: Application) : AnchorTask(TASK_NAME_ZERO) {
    val TAG ="SampleAppAnchorTask"
    override fun isRunOnMainThread(): Boolean {
        return false
    }

    override fun run() {
        Log.d(TAG, "run: AnchorTaskZero")
        BaseAppLication.init(context)
        initARouter(context)
//        initSystemInfoMsg(context)
    }



    private fun initARouter(mContext: Application) {
        if (BuildConfig.DEBUG) {
            // 打印日志
            ARouter.openLog()
            // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
            ARouter.openDebug()
        }

        // 初始化 ARouter
        ARouter.init(mContext)
    }



}

class AnchorTaskOne(val context: Application) : AnchorTask(TASK_NAME_ONE) {
    val TAG ="SampleAppAnchorTask"
    override fun isRunOnMainThread(): Boolean {
        return true
    }

    override fun run() {
        Log.d(TAG, "run: AnchorTaskOne")
        MMKV.initialize(context) //MMKV——基于 mmap 的高性能通用 key-value 组件
        initLifecycleOwner()
    }
    private fun initLifecycleOwner() {
        ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifecycleObserver(object :
            AppLifecycleCallback {
            override fun onStart() {
                LiveEventBus.get(LiveEventBusString.Home.SystemTime).post(null)
            }

            override fun onStop() {}
            override fun onCreate() {}
            override fun onPause() {}
            override fun onDestroy() {}
            override fun onResume() {}
            override fun onAny() {}
        }))
    }
}

class AnchorTaskTwo(val context: Application) : AnchorTask(TASK_NAME_TWO) {
    val TAG ="SampleAppAnchorTask"
    override fun isRunOnMainThread(): Boolean {
        return false
    }

    override fun run() {
        Log.d(TAG, "run: AnchorTaskTwo")
    }
}

class AnchorTaskThree : AnchorTask(TASK_NAME_THREE) {
    val TAG ="SampleAppAnchorTask"
    override fun isRunOnMainThread(): Boolean {
        return false
    }

    override fun run() {
        System.currentTimeMillis()
        try {
            Thread.sleep(300)
        } catch (e: Exception) {
        }
    }

}


class AnchorTaskFour : AnchorTask(TASK_NAME_FOUR) {
    override fun isRunOnMainThread(): Boolean {
        return false
    }

    override fun run() {
        System.currentTimeMillis()
        try {
            Thread.sleep(300)
        } catch (e: Exception) {
        }
    }

}

class AnchorTaskFive : AnchorTask(TASK_NAME_FIVE) {
    override fun isRunOnMainThread(): Boolean {
        return false
    }

    override fun run() {
        System.currentTimeMillis()
        try {
            Thread.sleep(300)
        } catch (e: Exception) {
        }
    }


}



